package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Hyzzqk;
import com.corpcredit.model.Jbqk;
import com.corpcredit.service.GenericManager;

public class HyzzqkForm extends BasePage implements Serializable {
    private GenericManager<Hyzzqk, Long> hyzzqkManager;
    private Hyzzqk hyzzqk = new Hyzzqk();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setHyzzqkManager(GenericManager<Hyzzqk, Long> hyzzqkManager) {
        this.hyzzqkManager = hyzzqkManager;
    }

    public Hyzzqk getHyzzqk() {
        return hyzzqk;
    }

    public void setHyzzqk(Hyzzqk hyzzqk) {
        this.hyzzqk = hyzzqk;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        hyzzqkManager.remove(hyzzqk.getId());
        addMessage("hyzzqk.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            hyzzqk = hyzzqkManager.get(id);
        } else {
            hyzzqk = new Hyzzqk();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (hyzzqk.getId() == null || hyzzqk.getId() == 0);
        hyzzqk.setJbqk( jbqk );
        hyzzqk.setUsername(getRequest().getRemoteUser());
        hyzzqk.setOptdate(new Date());
        hyzzqk.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        hyzzqkManager.save(hyzzqk);

        String key = (isNew) ? "hyzzqk.added" : "hyzzqk.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = hyzzqk.getId();
            return "edit";
        }
    }
} 